package com.cwx.mychat.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cwx.mychat.entity.constant.CodeConstant;
import com.cwx.mychat.entity.dto.TokenUserInfoDto;
import com.cwx.mychat.entity.po.UserContact;
import com.cwx.mychat.entity.po.UserContactApply;
import com.cwx.mychat.entity.po.UserInfo;
import com.cwx.mychat.exception.BusinessException;
import com.cwx.mychat.service.UserContactApplyService;
import com.cwx.mychat.service.UserContactCategoryService;
import com.cwx.mychat.service.UserContactService;
import com.cwx.mychat.service.UserInfoService;
import com.cwx.mychat.utils.R;
import com.cwx.mychat.utils.common.CommonUtil;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 作者： cwx
 * 时间： 2024/10/14 18:18
 * 版本： 1.0
 * 注释：
 */
@Validated
@RestController
@RequestMapping("/userContact")
public class UserContactController {

    @Autowired
    private UserContactService userContactService;

    @Autowired
    private UserContactApplyService userContactApplyService;

    @Autowired
    private UserContactCategoryService userContactCategoryService;


    /**
     * 获取联系人中的所有用户
     * @param userId
     * @return
     */
    @GetMapping("/getAllUser")
    public R getAllContactForUser(@RequestParam("userId") String userId){
       return userContactService.getAllContactForUser(userId);
    }

    /**
     * 获取联系人中所有群组
     * @param userId
     * @return
     */
    @GetMapping("/getAllGroupByCategory")
    public R getAllContactForGroup(@RequestParam("userId") String userId){
        return userContactService.getAllContactForGroup(userId);
    }

    /**
     * 获取联系人中所有群组
     * @param userId
     * @return
     */
    @GetMapping("/getAllGroup")
    public R getAllGroupByUserId(@RequestParam("userId") String userId){
        return userContactService.getAllGroupByUserId(userId);
    }

    /**
     * 更新remark备注
     * @param contactId
     * @param remark
     * @return
     */
    @RequestMapping("/updateRemark")
    public R updateRemark(@RequestParam("contactId") @NotNull String contactId,
                          @RequestParam("remark") @NotNull String remark) throws BusinessException {
        return userContactService.updateRemark(contactId, remark);
    }

    /**
     * 添加联系人
     * @param params
     * @return
     */
    @PostMapping("/addContact")
    public R addContact(@RequestBody Map<String, Object> params) throws BusinessException {
        String verifyInfo = (String) params.get("verifyInfo");
        String remark = (String) params.get("remark");
        Map group = (Map) params.get("group");
        Boolean permission = (Boolean) params.get("permission");
        String receiveId = (String) params.get("receiveId");
        String helloMsg = (String) params.get("helloMsg");
        System.out.println(params);
        //添加申请列表
        return userContactApplyService.addContact(verifyInfo, remark, group, permission, receiveId, helloMsg);
    }

    /**
     * 展示所有申请人
     * @return
     */
    @GetMapping("/showApply")
    public R showApplyContact(){
        return userContactApplyService.showApplyContact();
    }

    /**
     * 同意申请
     * @param body
     * @return
     */
    @PostMapping("/agreeApply")
    public R agreeApply(@RequestBody Map<String, Object> body) throws BusinessException {
        String applyUserId = (String) body.get("applyUserId");
        String receiveUserId = (String) body.get("receiveUserId");
        String applyId = (String) body.get("applyId");
        return userContactApplyService.agreeApply(applyUserId, receiveUserId, applyId);
    }

    /**
     * 拒绝申请
     * @param body
     */
    @PostMapping("/refuseApply")
    public R refuseApply(@RequestBody Map<String, Object> body) throws BusinessException {
        String applyUserId = (String) body.get("applyUserId");
        String receiveUserId = (String) body.get("receiveUserId");
        String applyId = (String) body.get("applyId");
        return userContactApplyService.refuseApply(applyUserId, receiveUserId, applyId);
    }

    /**
     * 查询所有分组
     * @param userId
     * @return
     */
    @GetMapping("/category")
    public R getAllGroup(@RequestParam("userId") String userId){
        return userContactCategoryService.getAllGroup(userId);
    }

    /**
     * 获取所有申请好友信息
     * @param userId
     * @return
     */
    @GetMapping("/getAllApply")
    public R getAllApply(@RequestParam("userId") String userId){
        return userContactApplyService.getAllApply(userId);
    }

    /**
     * 获取群组或者用户简单信息
     * @return
     */
    @GetMapping("/{contactId}")
    public R getGroupOrUserInfo(@PathVariable("contactId") String contactId){
        return userContactService.getGroupOrUserInfo(contactId);
    }
}
